package app.itgungnir.compose.wan.widgets

import android.content.res.Configuration
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Title
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import app.itgungnir.compose.wan.theme.WanComposePreviewTheme

@Composable
fun WanTextInput(
    state: MutableState<TextFieldValue>,
    label: String,
    loadingState: State<Boolean> = mutableStateOf(false)
) {
    TextField(
        modifier = Modifier.fillMaxWidth(),
        value = state.value,
        onValueChange = { state.value = it },
        singleLine = true,
        label = { Text(text = label) },
        colors = TextFieldDefaults.textFieldColors(
            textColor = MaterialTheme.colors.onBackground
        ),
        enabled = !loadingState.value
    )
}

@Composable
fun WanPasswordInput(
    state: MutableState<TextFieldValue>,
    label: String,
    loadingState: State<Boolean> = mutableStateOf(false)
) {
    val passwordVisibility = remember { mutableStateOf(false) }
    TextField(
        modifier = Modifier.fillMaxWidth(),
        value = state.value,
        onValueChange = { state.value = it },
        singleLine = true,
        label = { Text(text = label) },
        visualTransformation = when (passwordVisibility.value) {
            true -> VisualTransformation.None
            else -> PasswordVisualTransformation()
        },
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
        trailingIcon = {
            IconButton(onClick = { passwordVisibility.value = !passwordVisibility.value }) {
                Icon(
                    imageVector = when (passwordVisibility.value) {
                        true -> Icons.Filled.VisibilityOff
                        else -> Icons.Filled.Visibility
                    }, contentDescription = ""
                )
            }
        },
        colors = TextFieldDefaults.textFieldColors(
            textColor = MaterialTheme.colors.onBackground
        ),
        enabled = !loadingState.value
    )
}

@Composable
fun WanOutlinedInput(
    state: MutableState<TextFieldValue>,
    label: String,
    icon: ImageVector,
    loadingState: State<Boolean> = mutableStateOf(false)
) {
    OutlinedTextField(
        modifier = Modifier.fillMaxWidth(),
        value = state.value,
        onValueChange = { state.value = it },
        label = { Text(text = label) },
        leadingIcon = {
            Icon(
                imageVector = icon,
                contentDescription = "",
                tint = MaterialTheme.colors.onBackground
            )
        },
        singleLine = true,
        colors = TextFieldDefaults.textFieldColors(
            textColor = MaterialTheme.colors.onBackground
        ),
        enabled = !loadingState.value
    )
}

@Preview(name = "WanInput light mode", showBackground = true)
@Preview(name = "WanInput dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable
private fun WanInputPreview() {
    WanComposePreviewTheme {
        Column(modifier = Modifier.fillMaxWidth()) {
            val textInputValueState = remember { mutableStateOf(TextFieldValue()) }
            WanTextInput(state = textInputValueState, label = "测试文本输入框")
            val passwordInputValueState = remember { mutableStateOf(TextFieldValue()) }
            WanPasswordInput(state = passwordInputValueState, label = "测试密码输入框")
            val outlinedInputValueState = remember { mutableStateOf(TextFieldValue()) }
            WanOutlinedInput(state = outlinedInputValueState, label = "测试边框输入框", icon = Icons.Filled.Title)
        }
    }
}
